package com.example.administrator.myapplication.View.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.Util.SharedPreferencesUtil;
import com.example.administrator.myapplication.View.layout.MineItemView;

/**
 * Created by Administrator on 2019/10/8 0008.
 */

public class ProfileActivity extends AppCompatActivity implements MineItemView.OnRootClickListener,View.OnClickListener{
    private LinearLayout ll_root;
    private TextView tv_back;
    private User user;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            getWindow().setStatusBarColor(Color.parseColor("#d16d26"));
        }
        initView();

    }
    @Override
    protected void onRestart(){
        super.onRestart();
        super.recreate();
    }

    private void initView() {
        tv_back = findViewById(R.id.toolbar_back);
        user = SharedPreferencesUtil.getUser(this);
        ll_root = findViewById(R.id.ll_root);
        tv_back.setOnClickListener(this);
        ll_root.addView(new MineItemView(this)
                .init(R.drawable.ic_user_profile,"登录名",false,false)
                .setRightContent(user.getLoginName())
                .showRightContext(true)
                .setOnRootClickListener(this,1)
        );
        ll_root.addView(new MineItemView(this)
                .init(R.drawable.ic_user_profile,"用户名",false,true)
                .setRightContent(user.getUserName())
                .showRightContext(true)
                .setOnRootClickListener(this,2)
        );
        ll_root.addView(new MineItemView(this)
                .init(R.drawable.ic_user_profile,"性别",false,true)
                .setRightContent(user.getSex())
                .showRightContext(true)
                .setOnRootClickListener(this,3)
        );
        ll_root.addView(new MineItemView(this)
                .init(R.drawable.ic_user_profile,"电话",false,true)
                .setRightContent(user.getPhonenumber())
                .showRightContext(true)
                .setOnRootClickListener(this,4)
        );
        ll_root.addView(new MineItemView(this)
                .init(R.drawable.ic_user_profile,"Email",false,true)
                .setRightContent(user.getEmail())
                .showRightContext(true)
                .setOnRootClickListener(this,5)
        );
    }

    private void setIntent(Intent intent,String property,String value){
        intent.putExtra("id",user.getUserId());
        intent.putExtra("property",property);
        intent.putExtra("value",value);
    }

    @Override
    public void onRootClick(View view) {
        Intent intent;
        switch ((int)view.getTag()){
            case 1:
                break;
            case 2:
                intent = new Intent(this,EditProfileActivity.class);
                setIntent(intent,"用户名",user.getUserName());
                startActivity(intent);
                break;
            case 3:
                intent = new Intent(this,EditProfileActivity.class);
                setIntent(intent,"性别",user.getSex());
                startActivity(intent);
                break;
            case 4:
                intent = new Intent(this,EditProfileActivity.class);
                setIntent(intent,"电话",user.getPhonenumber());
                startActivity(intent);
                break;
            case 5:
                intent = new Intent(this,EditProfileActivity.class);
                setIntent(intent,"Email",user.getEmail());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.toolbar_back:
                finish();
                break;
        }
    }
}
